#include "StdAfx.h"
#include <string>
#include <Util/StringUtil.h>
#include "shedule_op.h"
#include <interface/data_op/table_wrapper.h>

namespace aot{ namespace data{

shedule_op_impl::shedule_op_impl(void)
{
}


shedule_op_impl::~shedule_op_impl(void)
{
}

bool 
shedule_op_impl::insert(const ishedule_info* p, int& new_id)
{
    if (!g_db_user) return false;

    std::string str_title			= p->title->c_str();
    std::string str_datetime_begin  = p->datetime_begin->c_str();
    std::string str_datetime_end	= p->datetime_end->c_str();
    std::string str_recv_number		= p->recv_number->c_str();
    std::string str_content			= p->content->c_str();

    remove_sin_quot(str_title);
    remove_sin_quot(str_datetime_begin);
    remove_sin_quot(str_datetime_end);
    remove_sin_quot(str_recv_number);
    remove_sin_quot(str_content);

    std::string sql_fmt = 
        "insert into shedule \
        (id,\
        type_call,\
        type_meeting,\
        type_private,\
        is_full_day,\
        notify_mode_tata,\
        notify_mode_sms,\
        notify_mode_email,\
        interval,\
        title,\
        datetime_begin,\
        datetime_end,\
        recv_number,\
        content) \
        values\
        (%d, %d, %d, %d, %d, %d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s' );";

    std::string sql_exe;
    int t_new_id = get_table_next_id("shedule");

    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        t_new_id,
        p->type_call,
        p->type_meeting,
        p->type_private,
        p->is_full_day,
        p->notify_mode_tata,
        p->notify_mode_sms,
        p->notify_mode_email,
        p->interval,

        str_title.c_str(),
        str_datetime_begin.c_str(),
        str_datetime_end.c_str(),
        str_recv_number.c_str(),
        str_content.c_str()
        );

    new_id = t_new_id;
    return g_db_user->exec(sql_exe.c_str());
}

bool 
shedule_op_impl::del(int id)
{
    std::string sql_fmt = "delete from shedule where id=%d;";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), id);
    return g_db_user->exec(sql_exe.c_str());
}

bool 
shedule_op_impl::update(const ishedule_info* p)
{
    if (!g_db_user) return false;

	std::string str_title			= p->title->c_str();
	std::string str_datetime_begin  = p->datetime_begin->c_str();
	std::string str_datetime_end	= p->datetime_end->c_str();
	std::string str_recv_number		= p->recv_number->c_str();
	std::string str_content			= p->content->c_str();
    
    remove_sin_quot(str_title);
    remove_sin_quot(str_datetime_begin);
    remove_sin_quot(str_datetime_end);
    remove_sin_quot(str_recv_number);
    remove_sin_quot(str_content);

    std::string sql_fmt = 
        "update shedule set \
        type_call=%d,\
        type_meeting=%d,\
        type_private=%d,\
        is_full_day=%d,\
        notify_mode_tata=%d,\
        notify_mode_sms=%d,\
        notify_mode_email=%d,\
        interval=%d,\
        title='%s',\
        datetime_begin='%s',\
        datetime_end='%s',\
        recv_number='%s',\
        content='%s' \
        where id = %d;";

    std::string sql_exe;
    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        p->type_call,
        p->type_meeting,
        p->type_private,
        p->is_full_day,
        p->notify_mode_tata,
        p->notify_mode_sms,
        p->notify_mode_email,
        p->interval,

        str_title.c_str(),
        str_datetime_begin.c_str(),
        str_datetime_end.c_str(),
        str_recv_number.c_str(),
        str_content.c_str(),
        p->id
        );
    return g_db_user->exec(sql_exe.c_str());
}

bool 
shedule_op_impl::read(int id, isqlite3_table** out)
{
    std::string sql_fmt = "select \
                          id,\
                          type_call,\
                          type_meeting,\
                          type_private,\
                          is_full_day,\
                          notify_mode_tata,\
                          notify_mode_sms,\
                          notify_mode_email,\
                          interval,\
                          title,\
                          datetime_begin,\
                          datetime_end,\
                          recv_number,\
                          content \
                          from shedule \
                          where id=%d;";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), id);
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

bool 
shedule_op_impl::read(int id, ishedule_info* out)
{
    aot::data::table_helper t_table;
    bool ret =false;
    if (this->read(id, t_table))
    {
        if ( t_table.get_rows_count() > 0 )
        {
            t_table.set_row_index(0);

            out->id = t_table.get_field_value_int("id");
            out->type_call = t_table.get_field_value_int("type_call");
            out->type_meeting = t_table.get_field_value_int("type_meeting");
            out->type_private = t_table.get_field_value_int("type_private");
            out->is_full_day = t_table.get_field_value_int("is_full_day");
            out->notify_mode_tata = t_table.get_field_value_int("notify_mode_tata");
            out->notify_mode_sms = t_table.get_field_value_int("notify_mode_sms");
            out->notify_mode_email = t_table.get_field_value_int("notify_mode_email");
            out->interval = t_table.get_field_value_int("interval");

            out->title->assign(t_table.get_field_value_str("title").c_str());
            out->datetime_begin->assign(t_table.get_field_value_str("datetime_begin").c_str());
            out->datetime_end->assign(t_table.get_field_value_str("datetime_end").c_str());
            out->recv_number->assign(t_table.get_field_value_str("recv_number").c_str());
            out->content->assign(t_table.get_field_value_str("content").c_str());

            ret = true;
        }
    }
    return ret;
}

bool 
shedule_op_impl::read(int id, ishedule_info** out)
{
    aot::data::ishedule_info* obj = new aot::data::shedule_info_impl();
    *out = obj;
    return this->read(id, obj);
}


}} /* end namespace aot/data*/